<?php

use console\db\Migration;
use yii\helpers\ArrayHelper;
use yii\db\Query;

/**
 * 用户性别信息转移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300000_000012_t_user_meta_format_gender extends Migration {

    /**
     * @inheritdoc
     */
    public $tableName = '{{%user_authent}}';

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $genders = [1, 2];
        foreach ($genders as $gender) {
            $query = (new Query())->from($this->tableName)->select([
                'id',
            ])->where([
                'and',
                ['gender' => $gender],
                ['>=', 'status', 0]
            ]);
            $this->handleProgress($query, [$this, 'evenDule'], $gender);
        }
    }

    /**
     * 处理结果
     * @param Migration $migration
     * @param array $rows
     * @param integer $gender
     */
    public static function evenDule($migration, $rows, $gender) {
        $ids = ArrayHelper::getColumn($rows, 'id');
        if (empty($ids)) {
            return;
        }
        $migration->db->createCommand()->update('{{%user_meta}}', [
            'gender' => $gender
        ], ['id' => $ids])->execute();
    }

}
